<template>
  <!-- 个人信息页面 -->
  <div>
    <div id="header">
      <el-row :gutter="20">
        <el-col :span="6" :offset="6">
          <div class="demo-type">
            <el-avatar :size="80" src="https://empty" @error="errorHandler">
              <img :src="avatar" />
            </el-avatar></div
        ></el-col>
        <el-col :span="6" :offset="6">
          <div class="xiugtx">
            <el-button type="success" plain @click="Upimg">修改头像</el-button>
          </div></el-col
        >
      </el-row>
    </div>
    <el-divider></el-divider>
    <div id="context">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="登录用户">
          <div>{{ user.username }}</div>
        </el-form-item>
        <el-form-item label="用户姓名">
          <div>{{ name }}</div>
        </el-form-item>
        <el-form-item label="用户手机">
          <div>{{ user.telephone }}</div>
        </el-form-item>
        <el-form-item label="用户性别">
          <div v-if="user.gender == 'male'">
            <span>男</span> <i class="el-icon-male" type="danger"></i>
          </div>
          <div v-else>
            <span>女</span> <i class="el-icon-female" type="danger"></i>
          </div>
        </el-form-item>
        <el-form-item label="用户角色">
          <div>{{ roles[0] }}</div>
        </el-form-item>
        <el-form-item label="注册时间">
          <div>{{ user.certificationTime }}</div>
        </el-form-item>
        <el-form-item label="邮箱">
          <div>{{ user.email }}</div>
        </el-form-item>
        <el-form-item label="用户状态">
          <div>
            <el-button type="success" disabled>{{ user.status }}</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <div class="booter">
      <el-row :gutter="20">
        <el-col :span="6" :offset="18">
          <el-button type="success" plain @click="upInfo"
            >修改个人信息</el-button
          ></el-col
        >
      </el-row>
    </div>

    <!-- 修改头像模态框 -->
    <el-dialog title="修改头像" :visible.sync="ImgdialogVisible" width="30%">
      <el-upload
        class="avatar-uploader"
        :action="uploadFileURL"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="upImageForm.userface" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ImgdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upImage">确定修改</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户信息的模态框 -->
    <el-dialog title="修改个人信息" :visible.sync="dialogInfoFormVisible">
      <el-form
        :model="upInfoForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 登录用户 -->
        <el-form-item label="登录用户" prop="username">
          <el-input v-model="upInfoForm.username" disabled></el-input>
        </el-form-item>

        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="upInfoForm.realname" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="upInfoForm.gender" label="male">男</el-radio>
          <el-radio v-model="upInfoForm.gender" label="female">女</el-radio>
        </el-form-item>
        <!-- 用户手机 -->
        <el-form-item label="用户手机" prop="telephone">
          <el-input v-model="upInfoForm.telephone"></el-input>
        </el-form-item>
        <!-- 用户邮箱 -->
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="upInfoForm.email"></el-input>
        </el-form-item>
        <!-- 出生日期 -->
        <el-form-item label="出生日期" required>
          <el-form-item prop="birth">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="upInfoForm.birth"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogInfoFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upUserInfoForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { mapActions } from "vuex";
import { get, post } from "@/utils/request";
import moment from "moment";
//引入文件上传路径 和文件预览的路径
import { showFileURL, uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      uploadFileURL,
      //  对用户信息修改的模态框
      dialogInfoFormVisible: false,
      // 对用户头像修改的模态框
      ImgdialogVisible: false,
      imageUrl: "",
      // 对头像修改的表单
      upImageForm: {},
      // 对用户信息修改的表单
      upInfoForm: {
        username: "",
        realname: "",
        gender: "",
        telephone: "",
        birth: "",
        email: "",
      },
      //对用户信息修改的表单的验证
      rules: {
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        gender: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["avatar", "name", "introduction", "roles", "user"]),
  },
  methods: {
    // 头像图片加载失败
    errorHandler() {
      return true;
    },
    // 对用户信息进行更新
    ...mapActions("user", ["getInfo"]),
    // 点击打开修改头像模态框
    Upimg() {
      this.ImgdialogVisible = true;
      this.upImageForm.id = this.user.id;
      this.upImageForm.userface = this.user.userFace;
    },
    // 提交用户头像
    async upImage() {
      let res = await post("/baseUser/alterUserface", this.upImageForm);
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
      }
      this.ImgdialogVisible = false;
      this.getInfo();
    },
    // 上传成功后回调的
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.upImageForm.userface = showFileURL + res.data.id;
    },
    // 在上传头像前做的操作
    beforeAvatarUpload(file) {
      const isLt3M = file.size / 1024 / 1024 < 3;
      if (!isLt3M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
    },
    // 打开用户信息修改模态框
    upInfo() {
      this.dialogInfoFormVisible = true;
      this.upInfoForm = { ...this.user };
      console.log(this.upInfoForm);
    },
    // 修改用户信息
    upUserInfoForm() {
      this.upInfoForm.birth = moment(this.upInfoForm.birth).valueOf();
      // console.log(this.upInfoForm, "--------");

      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          let res = await post("/baseUser/saveOrUpdate", this.upInfoForm);
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
          }

          this.dialogInfoFormVisible = false;
          this.getInfo();
        } else {
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {},
  mounted() {},
};
</script>
<style scoped>
.xiugtx {
  line-height: 80px;
}
/* 头像修改 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>